package com.ruoyi.projects.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.constant.BuConstants;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.projects.domain.SlOrderDetail;
import com.ruoyi.projects.mapper.SlOrderDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.projects.mapper.SlOrderMapper;
import com.ruoyi.projects.domain.SlOrder;
import com.ruoyi.projects.service.ISlOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 森理订单Service业务层处理
 * 
 * @author yangz
 * @date 2024-10-23
 */
@Service
public class SlOrderServiceImpl implements ISlOrderService 
{
    @Autowired
    private SlOrderMapper slOrderMapper;

    @Autowired
    private SlOrderDetailMapper slOrderDetailMapper;

    public static String generateOrderNumber() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String timeStamp = sdf.format(new Date());
        Random random = new Random();
        int randomNumber = random.nextInt(1000);  // 生成0到999之间的随机数
        return timeStamp + String.format("%03d", randomNumber);
    }

    /**
     * 查询森理订单
     * 
     * @param id 森理订单主键
     * @return 森理订单
     */
    @Override
    public SlOrder selectSlOrderById(Long id)
    {
        return slOrderMapper.selectSlOrderById(id);
    }

    /**
     * 查询森理订单列表
     * 
     * @param slOrder 森理订单
     * @return 森理订单
     */
    @Override
    public List<SlOrder> selectSlOrderList(SlOrder slOrder)
    {
        return slOrderMapper.selectSlOrderList(slOrder);
    }

    /**
     * 新增森理订单
     * 
     * @param slOrder 森理订单
     * @return 结果
     */
    @Override
    public int insertSlOrder(SlOrder slOrder)
    {
        slOrder.setCreateTime(DateUtils.getNowDate());
        return slOrderMapper.insertSlOrder(slOrder);
    }

    /**
     * 修改森理订单
     * 
     * @param slOrder 森理订单
     * @return 结果
     */
    @Override
    public int updateSlOrder(SlOrder slOrder)
    {
        slOrder.setUpdateTime(DateUtils.getNowDate());
        return slOrderMapper.updateSlOrder(slOrder);
    }

    /**
     * 批量删除森理订单
     * 
     * @param ids 需要删除的森理订单主键
     * @return 结果
     */
    @Override
    public int deleteSlOrderByIds(Long[] ids)
    {
        return slOrderMapper.deleteSlOrderByIds(ids);
    }

    /**
     * 删除森理订单信息
     * 
     * @param id 森理订单主键
     * @return 结果
     */
    @Override
    public int deleteSlOrderById(Long id)
    {
        return slOrderMapper.deleteSlOrderById(id);
    }

    /**
     * 创建订单和订单详情记录
     * @param slOrder
     * @return
     */
    @Override
    @Transactional
    public int addSlOrder(SlOrder slOrder) {
        try {
            slOrder.setOrderNo(generateOrderNumber());
            slOrder.setCreateTime(DateUtils.getNowDate());
            int i = slOrderMapper.insertSlOrder(slOrder);
            if (i>0){
                //订单记录成功，保存订单明细
                SlOrderDetail slOrderDetai=new SlOrderDetail();
                slOrderDetai.setCustomId(slOrder.getCustomId());
                slOrderDetai.setOrderId(slOrder.getId());
                slOrderDetai.setProductId(slOrder.getProductId());
                slOrderDetai.setProductName(slOrder.getProductName());
                slOrderDetai.setServiceTimes(slOrder.getServiceTimes());
                slOrderDetai.setServiceLeftTimes(slOrder.getServiceLeftTimes());
                slOrderDetai.setServiceStatus(BuConstants.SERVICE_STATUS_NONE);
                slOrderDetai.setCreateTime(DateUtils.getNowDate());
                int i1 = slOrderDetailMapper.insertSlOrderDetail(slOrderDetai);
                if (i1>0){
                    return 1;
                }else {
                    throw new BaseException("订单明细记录失败");
                }
            }
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }
}
